class Manager_actions

	def initialize(maxsize = 5)
		@maxsize = maxsize
		@rate_attack = 10
		@rate_calc = 20
		@actionlist = Array.new

		create_actions(maxsize)
	end

	def create_actions(number)
		number.times {|n|
			action = create(@rate_attack, @rate_calc)
			@actionlist.push(action)
		}

		return(number)
	end

	def set_autoupdate(interval)
		t = Thread.new {
			while (true) do
				printf("Current actions: %d\n", count)
				printf("Update  actions: %s\n", update.join(", ").to_s)
				sleep(interval)
			end
		}
	end

	#####
	def create(rate_attack, rate_calc)
		r = rand((rate_attack+rate_calc)*10)

		action = Hash.new
		if (r < rate_attack*10) then
			# Create "ATTACK" action
			action['type'] = "attack"
			action['data'] = "COMMAND"
		else
			# Create "CALC" action
			action['type'] = "calc"
			action['data'] = "COMMAND"
		end

		return action
	end

	def count
		n = 0
		@actionlist.each {|data|
			n = n + 1 if (data != nil)
		}
		return (n)
	end

	def update
		updates = Array.new
		@maxsize.times {|n|
			if (@actionlist[n] == nil) then
				action = create(@rate_attack, @rate_calc)
				@actionlist[n] = action

				updates.push(n)
			end
		}
		return (updates)
	end

	def select(position)
		return(@actionlist[position])
	end

	def delete(position)
		@actionlist[position] = nil
		# @actionlist.delete_at(position)
	end

	def size
		return count
	end

	attr_accessor :maxsize
end

#action = CloudWar_action.new
#p action.size
#action.delete(2)
#action.delete(4)
#p action.size
#p action.update
#
#action.set_autoupdate(2)
#
#sleep(10)
